using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WF1.lab06
{
    public partial class frmLogin : Form
    {
        string strConnection;
        string sql;
        SqlConnection con;
        SqlCommand com;
        SqlDataReader reader;

        int loginTime;
        public frmLogin()
        {
            InitializeComponent();
            loginTime = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtUsername.Text == "")
            {
                MessageBox.Show("You must entry username.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUsername.Focus();
            }
            else if (txtPassword.Text.Length < 6)
            {
                MessageBox.Show("Password has from 6 to 20 character.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPassword.Focus();
            }
            else
            {
                try
                {
                    sql = "select * from account where username=@u and password=@p";
                    com = new SqlCommand(sql, con);
                    com.Parameters.AddWithValue("@u", txtUsername.Text);
                    com.Parameters.AddWithValue("@p", txtPassword.Text);

                    reader = com.ExecuteReader();

                    if (!reader.HasRows)
                    {
                        MessageBox.Show("Invalid username or password.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        loginTime++;
                    }
                    else
                    {
                        reader.Read();

                        string username = reader["username"].ToString();
                        string pass = reader["password"].ToString();

                        if (txtUsername.Text == username && txtPassword.Text == pass) 
                        {
                            MessageBox.Show("Login sucessful.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Application.Exit();
                        }
                        else
                        {
                            MessageBox.Show("Username and password is case sensitive.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            loginTime++;
                        }
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            if (loginTime == 3)
            {
                MessageBox.Show("Login 3 time fail.\n Application will exited.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Application.Exit();
            }

        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            try
            {
                strConnection = global::WF1.Properties.Settings.Default.strConnection;
                con = new SqlConnection(strConnection);

                if (con.State != ConnectionState.Open)
                {
                    con.Open();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not connect to database.\nError:\n"+ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                Application.Exit();
            }
        }
    }
}